package com.yic.module.mes.controller.admin.itemproduce;

import cn.hutool.core.util.ObjectUtil;
import com.yic.module.base.api.center.CenterApi;
import com.yic.module.base.api.center.dto.CenterRespDTO;
import com.yic.module.base.api.factory.FactoryApi;
import com.yic.module.base.api.factory.dto.FactoryRespDTO;
import com.yic.module.base.api.item.ItemApi;
import com.yic.module.base.api.item.dto.ItemRespDTO;
import com.yic.module.base.api.workshop.WorkshopApi;
import com.yic.module.base.api.workshop.dto.WorkshopRespDTO;
import com.yic.module.system.api.dept.DeptApi;
import com.yic.module.system.api.dept.dto.DeptRespDTO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.yic.framework.common.pojo.PageResult;
import com.yic.framework.common.pojo.CommonResult;
import static com.yic.framework.common.pojo.CommonResult.success;

import com.yic.framework.excel.core.util.ExcelUtils;

import com.yic.framework.operatelog.core.annotations.OperateLog;
import static com.yic.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.yic.module.mes.controller.admin.itemproduce.vo.*;
import com.yic.module.mes.dal.dataobject.itemproduce.ItemProduceDO;
import com.yic.module.mes.convert.itemproduce.ItemProduceConvert;
import com.yic.module.mes.service.itemproduce.ItemProduceService;

@Tag(name = "管理后台 - 产品生产信息")
@RestController
@RequestMapping("/mes/item-produce")
@Validated
public class ItemProduceController {

    @Resource
    private ItemProduceService itemProduceService;
    @Resource
    private ItemApi itemApi;
    @Resource
    private FactoryApi factoryApi;
    @Resource
    private WorkshopApi workshopApi;

    @PostMapping("/create")
    @Operation(summary = "创建产品生产信息")
    @PreAuthorize("@ss.hasPermission('mes:item-produce:create')")
    public CommonResult<Long> createItemProduce(@Valid @RequestBody ItemProduceCreateReqVO createReqVO) {
        return success(itemProduceService.createItemProduce(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新产品生产信息")
    @PreAuthorize("@ss.hasPermission('mes:item-produce:update')")
    public CommonResult<Boolean> updateItemProduce(@Valid @RequestBody ItemProduceUpdateReqVO updateReqVO) {
        itemProduceService.updateItemProduce(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除产品生产信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mes:item-produce:delete')")
    public CommonResult<Boolean> deleteItemProduce(@RequestParam("id") Long id) {
        itemProduceService.deleteItemProduce(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得产品生产信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mes:item-produce:query')")
    public CommonResult<ItemProduceRespVO> getItemProduce(@RequestParam("id") Long id) {
        ItemProduceDO itemProduce = itemProduceService.getItemProduce(id);
        return success(ItemProduceConvert.INSTANCE.convert(itemProduce));
    }

    @GetMapping("/info")
    @Operation(summary = "获得产品生产信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mes:item-produce:query')")
    public CommonResult<ItemProduceRespVO> getItemProduceInfo(@RequestParam("id") Long id) {
        ItemProduceRespVO itemProduceInfo = itemProduceService.getItemProduceInfo(id);
        // 组装产品生产信息
        ItemRespDTO item = itemApi.getItem(itemProduceInfo.getItemId());
        if (ObjectUtil.isNotEmpty(item)) {
            itemProduceInfo.setItemCode(item.getCode());
            itemProduceInfo.setItemName(item.getName());
            itemProduceInfo.setSpec(item.getSpec());
            itemProduceInfo.setDrawCode(item.getDrawCode());
        }
        return success(itemProduceInfo);
    }

    @GetMapping("/list")
    @Operation(summary = "获得产品生产信息列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('mes:item-produce:query')")
    public CommonResult<List<ItemProduceRespVO>> getItemProduceList(@RequestParam("ids") Collection<Long> ids) {
        List<ItemProduceDO> list = itemProduceService.getItemProduceList(ids);
        return success(ItemProduceConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得产品生产信息分页")
    @PreAuthorize("@ss.hasPermission('mes:item-produce:query')")
    public CommonResult<PageResult<ItemProduceRespVO>> getItemProducePage(@Valid ItemProducePageReqVO pageVO) {
        PageResult<ItemProduceDO> pageResult = itemProduceService.getItemProducePage(pageVO);
        PageResult<ItemProduceRespVO> resp = ItemProduceConvert.INSTANCE.convertPage(pageResult);
        resp.getList().forEach(itemProduce -> {
            //封装工厂信息
            FactoryRespDTO factory = factoryApi.getFactory(itemProduce.getFactoryId());
            if (ObjectUtil.isNotEmpty(factory)) {
                itemProduce.setFactoryName(factory.getName());
            }
            //封装生产车间
            WorkshopRespDTO workshop = workshopApi.getWorkshop(itemProduce.getWorkshopId());
            if (ObjectUtil.isNotEmpty(workshop)) {
                itemProduce.setWorkshopName(workshop.getName());
            }
            //封装产品信息
            ItemRespDTO item = itemApi.getItem(itemProduce.getItemId());
            if (ObjectUtil.isNotEmpty(item)) {
                itemProduce.setItemName(item.getName());
                itemProduce.setItemCode(item.getCode());
                itemProduce.setSpec(item.getSpec());
                itemProduce.setDrawCode(item.getDrawCode());
            }
        });
        return success(resp);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出产品生产信息 Excel")
    @PreAuthorize("@ss.hasPermission('mes:item-produce:export')")
    @OperateLog(type = EXPORT)
    public void exportItemProduceExcel(@Valid ItemProduceExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ItemProduceDO> list = itemProduceService.getItemProduceList(exportReqVO);
        // 导出 Excel
        List<ItemProduceExcelVO> datas = ItemProduceConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "产品生产信息.xls", "数据", ItemProduceExcelVO.class, datas);
    }

}
